import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:xindong/common/xindong_common_vm.dart';
import 'package:xindong/widgets/container/xindong_scroll_notification_container.dart';

class XinDongGetBuilder<T extends XinDongCommonVM> extends StatelessWidget {
  final String? tag;
  final Widget Function(T viewClass) builder;
  final Function(GetBuilderState<T>)? initState;

  const XinDongGetBuilder({Key? key, required this.builder, this.initState, this.tag}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<T>(
      tag: tag,
      initState: initState,
      builder: this.builder,
    );
  }
}

/// 监听数据自动刷新
class XinDongRefreshContainer<T extends XinDongCommonVM> extends StatelessWidget {
  final T viewClass;
  final Widget? child;
  final Function(double currentPixel, double totalPixel)? onScrollStart; // 监听滚动开始
  final Function(double currentPixel, double totalPixel)? onScrollEnd; // 监听滚动结束

  const XinDongRefreshContainer({Key? key, required this.viewClass, required this.child, this.onScrollStart, this.onScrollEnd}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return XinDongScrollNotificationContainer(
      yemian: 2,
      onLoadMore: viewClass.isLoadMoreData,
      onScrollStart: onScrollStart,
      onScrollEnd: onScrollEnd,
      child: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        controller: viewClass.refreshController,
        onRefresh: viewClass.refreshData,
        onLoading: viewClass.loadMoreData,
        child: child,
      ),
    );
  }
}
